package com.cskt.petums.controller;

import com.cskt.common.constants.Constants;
import com.cskt.common.entity.pojo.User;
import com.cskt.common.entity.vo.ResultVO;
import com.cskt.common.entity.vo.StatusEnum;
import com.cskt.common.entity.vo.UserInfoVO;
import com.cskt.petums.service.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户相关的控制器
 *
 * @author admin
 * @date 2023/04/14 21:11
 **/
@RestController
@RequestMapping(value = "/user")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Resource
    private UserService userService;

    @Resource
    private ObjectMapper objectMapper;

    /**
     * 登录
     *
     * @param phone 手机号
     * @param sms   验证码
     * @return
     */
    @PostMapping(value = "/login")
    public ResultVO<Map<String, Object>> login(String phone, String sms) {
        // 判断手机号是否正确
        if (!checkPhone(phone)) {
            return ResultVO.error(StatusEnum.FAIL_UMS_PHONE_ERROR);
        }
        String token = userService.login(phone, sms);
        if (!StringUtils.hasText(token)) {
            return ResultVO.error();
        }
        Map<String, Object> tokenMap = new HashMap<>();
        tokenMap.put(Constants.Auth.HEADER_TOKEN_KEY_NAME, token);
        return ResultVO.ok(tokenMap);
    }

    @GetMapping(value = "/getUserById/{userId}")
    public ResultVO<User> getUserById(@PathVariable Long userId) {
        //模拟一次网络延迟
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        User user = userService.getUserById(userId);
        return ResultVO.ok(user);
    }
    
    @GetMapping(value = "/sentinel2")
    public String sentinel2() {
        return "测试高并发下的问题";
    }

    /**
     * 根据token获取用户信息
     *
     * @param token 从请求头中获取token
     * @return
     */
    @GetMapping(value = "/getUserByToken")
    public ResultVO<UserInfoVO> getUserByToken(@RequestHeader(value = "token") String token) {
        log.info("来自远程调用");
        if (!StringUtils.hasText(token)) {
            return ResultVO.error(StatusEnum.AUTH_TOKEN_EMPTY);
        }
        UserInfoVO userInfoVO = userService.getUserInfoByToken(token);
        if (ObjectUtils.isEmpty(userInfoVO)) {
            return ResultVO.error(StatusEnum.AUTH_USER_NOT_LOGIN);
        }
        return ResultVO.ok(userInfoVO);
    }

    @GetMapping(value = "/logout")
    public ResultVO logout() {
        return ResultVO.ok();
    }
    
    
    private boolean checkPhone(String phone) {
        return phone.matches(Constants.Regular.PHONE_REGULAR_EXPRESSION);
    }
    


}
